package com.weifangfirecontrolformal.weifangfirecontrolformal.web.service.internetofthings.autonet;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.weifangfirecontrolformal.weifangfirecontrolformal.common.base.BaseService;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.model.RespEntity;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.utils.requestutil.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class AutoNetService extends BaseService {

	private static final Logger logger = LoggerFactory.getLogger(AutoNetService.class);



	/**
	 * 首页数据
	 * @param
	 * @return
	 */
	public RespEntity getHomePageData(JSONObject jsonParam) {
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		//汇总数据
		Map<String,Object> allMap = new HashMap<>();
		//今日报警
		jsonParam.put("deviceClassify","5");
		com.alibaba.fastjson.JSONObject todayAlarmObject = makeData(jsonParam,"fireFightingiotJava","config","todayAlarm","fireFightingIOT");
		if(StrUtil.isNotBlank(todayAlarmObject.getString("methodCodeFlag"))) return RespEntity.expire();
		if(StrUtil.isNotBlank(todayAlarmObject.getString("methodErrorFlag"))) return RespEntity.fail(todayAlarmObject.getString("msg"));
		Map<String,Object> todayAlarm = new HashMap<>();
		todayAlarm.put("todaySystemEventCount",todayAlarmObject.get("todaySystemEventCount"));
		todayAlarm.put("todayFaultCount",todayAlarmObject.get("todayFaultCount"));
		todayAlarm.put("todayFireAlarmCount",todayAlarmObject.get("todayFireAlarmCount"));
		todayAlarm.put("allCount",todayAlarmObject.get("allCount"));
		allMap.put("todayAlarm",todayAlarm);
		//一周报警
		JSON weekAlarmObject = makeDataForJSON(jsonParam,"fireFightingiotJava","config","weekAlarm","fireFightingIOT");
		if(weekAlarmObject instanceof com.alibaba.fastjson.JSONObject){
			com.alibaba.fastjson.JSONObject weekAlarmObjectN = (com.alibaba.fastjson.JSONObject)weekAlarmObject;
			if(StrUtil.isNotBlank(weekAlarmObjectN.getString("methodCodeFlag"))){
				return RespEntity.expire();
			}
			return RespEntity.fail(((com.alibaba.fastjson.JSONObject) weekAlarmObject).getString("msg"));
		}else{
			allMap.put("weekAlarmList",weekAlarmObject);
		}
		//设备统计
		jsonParam.put("deviceClassify","5");
		com.alibaba.fastjson.JSONObject todayDeviceCountByStateObject = makeData(jsonParam,"fireFightingiotJava","config","todayDeviceCountByState","device");
		if(StrUtil.isNotBlank(todayDeviceCountByStateObject.getString("methodCodeFlag"))) return RespEntity.expire();
		if(StrUtil.isNotBlank(todayDeviceCountByStateObject.getString("methodErrorFlag"))) return RespEntity.fail(todayDeviceCountByStateObject.getString("msg"));
		allMap.put("todayDeviceCountByState",todayDeviceCountByStateObject);
		if(jsonParam.getStr("userType").equals("1")){
			//报警单位分析
			JSON alarmBusinessAnalysisObject = makeDataForJSON(jsonParam,"fireFightingiotJava","config","alarmBusinessAnalysis","fireFightingIOT");
			if(alarmBusinessAnalysisObject instanceof com.alibaba.fastjson.JSONObject){
				com.alibaba.fastjson.JSONObject JSONObjectalarmBusinessAnalysisObjectNN = (com.alibaba.fastjson.JSONObject)alarmBusinessAnalysisObject;
				if(StrUtil.isNotBlank(JSONObjectalarmBusinessAnalysisObjectNN.getString("methodCodeFlag"))){
					return RespEntity.expire();
				}
				return RespEntity.fail(((com.alibaba.fastjson.JSONObject) alarmBusinessAnalysisObject).getString("msg"));
			}else{
				allMap.put("alarmBusinessAnalysisList",alarmBusinessAnalysisObject);
			}
			//联网单位统计
			com.alibaba.fastjson.JSONObject businessCount = makeData(jsonParam,"businessJava","config","businessCount","business");
			if(StrUtil.isNotBlank(businessCount.getString("methodCodeFlag"))) return RespEntity.expire();
			if(StrUtil.isNotBlank(businessCount.getString("methodErrorFlag"))) return RespEntity.fail(businessCount.getString("msg"));
			allMap.put("businessCount",businessCount);
		}
		return RespEntity.success(allMap);
	}

	/**
	 * 实时报警列表
	 * @param
	 * @return
	 */
	public RespEntity realTimeAlarm(JSONObject jsonParam) {
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		jsonParam.put("deviceClassify","5");
		return fireFightingiotJava(jsonParam,"realTimeAlarm","fireFightingIOT");
	}

	/**
	 * 一周报警
	 * @param
	 * @return
	 */
	public RespEntity weekAlarm(JSONObject jsonParam) {
		jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
		jsonParam.put("deviceClassify","5");
		return RespEntity.success(makeDataForJSON(jsonParam,"fireFightingiotJava","config","weekAlarm","fireFightingIOT"));
	}
}
